<?php
class menus {
	private $db;
	function menus() {
	}
	function getMain(){
		$pid=$this->jieku->var[pid];
		$db=$this->jieku->getDB();
		$view=$this->jieku->getView();
		if (intval($this->jieku->u['root']) != 1) {
			$powers = $this->jieku->u['control'];
			$power = implode(',', $powers['menus']);
			$sql = "SELECT * FROM `sys_menus` WHERE `id` IN ($power) ORDER BY `id` ASC";
		} else {
			$sql=JDBTool::select('*',"sys_menus","pid=0 order by id ASC",true);
		}
		$db->query($sql);
		$view->add("menuList", $db->getAll());
	}
	function get(){
		$db=$this->jieku->getDB();
		$view=$this->jieku->getView();
		$pid=$this->jieku->var[pid];
		if (!$pid)$pid=0;
		App::loadMod("Menu");
		$menu=new Menu($this->jieku);
		$view->add("menuTree",$menu->getMenuTree($pid));
	}
	function edit(){
		$view=$this->jieku->getView();
		$db=$this->jieku->getDB();
		App::loadMod("Menu");
		$menu=new Menu($this->jieku);
		$view->add("menuList",$menu->getTree(0));
		$sql=JDBTool::select(array("id,title"),"sys_menus"," 1=1 order by id ASC",true);
		$db->query($sql,PDO::FETCH_ASSOC);
		$result=$db->getAll();
		$rootMenu[0]="根節點";
		foreach ($result as $r){
			$rootMenu[$r[id]]=$r[title];
		}
		$view->add("rootMenu",$rootMenu);
	}
	function create(){
		$db=$this->jieku->getDB();
		$pid=$this->jieku->var[pid];
		App::loadMod("Menu");
		$menu=new Menu($this->jieku);
		$menu->pid=$pid;
		$menu->insert();
		App::loadLib("JieKu.Cache.JMyCache");
		JMyCache::clear("sys.menu");
		echo '{"success":true,"id":"'.$menu->id.'","url":"'.$menu->url.'","title":"'.$menu->title.'","pid":"'.$menu->pid.'","comment":"'.$menu->comment.'"}';
	}
	function delete(){
		$id=$this->jieku->var[id];
		$db=$this->jieku->getDB();
		$db->exec(JDBTool::delete("sys_menus",array("id"=>$id),true));
		App::loadLib("JieKu.Cache.JMyCache");
		JMyCache::clear("sys.menu");
		echo "true";
	}
	function update(){
		$id=$this->jieku->var[id];
		$db=$this->jieku->getDB();
		$db->exec(JDBTool::update("sys_menus",$this->jieku->var[menu],array("id"=>$id)));
		JMyCache::clear("sys.menu");
		echo "true";
	}
}
?>